package org.szgb.console.sys.custom.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysCustomFormRecord extends BeanEntity<SysCustomFormRecord> {
    @Serial
    private static final long serialVersionUID = 1L;
    private SysCustomForm sysCustomForm;

    public SysCustomFormRecord() {
    }

    public SysCustomFormRecord(String id) {
        this.setId(id);
    }

    public SysCustomFormRecord(SysCustomFormRecord.SysCustomFormRecordFindDto SysCustomFormRecordFindDto){
        this.setId(SysCustomFormRecordFindDto.getId());
        if(SysCustomFormRecordFindDto.getSysCustomFormId() != null){
            this.setSysCustomForm(new SysCustomForm(SysCustomFormRecordFindDto.getSysCustomFormId()));
        }
        this.setPageDto(SysCustomFormRecordFindDto.getPageDto());
    }

    public SysCustomFormRecord(SysCustomFormRecord.SysCustomFormRecordSaveDto SysCustomFormRecordSaveDto){
        this.setId(SysCustomFormRecordSaveDto.id);
        if(SysCustomFormRecordSaveDto.sysCustomFormId != null){
            this.setSysCustomForm(new SysCustomForm(SysCustomFormRecordSaveDto.sysCustomFormId));
        }
    }

    public SysCustomFormRecord.SysCustomFormRecordView toSysCustomFormRecordView() {
        return new SysCustomFormRecord.SysCustomFormRecordView(this);
    }

    @Data
    @Schema(title = "系统信息", description = "系统信息")
    public static class SysCustomFormRecordView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "表单id")
        private String sysCustomFormId;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysCustomFormRecordView(SysCustomFormRecord SysCustomFormRecord) {
            this.id = SysCustomFormRecord.getId();
            if(SysCustomFormRecord.getSysCustomForm() != null) {
                this.sysCustomFormId = SysCustomFormRecord.getSysCustomForm().getId();
            }
            this.createdAt = SysCustomFormRecord.getCreatedAt();
            this.updatedAt = SysCustomFormRecord.getUpdatedAt();
            if (SysCustomFormRecord.getCreatedBy() != null) {
                this.createdById = SysCustomFormRecord.getCreatedBy().getId();
                this.createdByName = SysCustomFormRecord.getCreatedBy().getName();
            }
            if (SysCustomFormRecord.getUpdatedBy() != null) {
                this.updatedById = SysCustomFormRecord.getUpdatedBy().getId();
                this.updatedByName = SysCustomFormRecord.getUpdatedBy().getName();
            }
        }

        public static List<SysCustomFormRecord.SysCustomFormRecordView> getViewListFromList(List<SysCustomFormRecord> SysCustomFormRecordList) {
            return SysCustomFormRecordList.stream().map(SysCustomFormRecord.SysCustomFormRecordView::new).toList();
        }
    }

    @Data
    @Schema(title = "系统查询表单", description = "系统查询参数")
    public static class SysCustomFormRecordFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "表单id")
        private String sysCustomFormId;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysCustomFormRecord toSysCustomFormRecord() {
            return new SysCustomFormRecord(this);
        }
    }

    @Data
    @Schema(title = "系统保存表单", description = "系统保存参数")
    public static class SysCustomFormRecordSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "表单id")
        private String sysCustomFormId;

        public SysCustomFormRecord toSysCustomFormRecord() {
            return new SysCustomFormRecord(this);
        }
    }
}
